/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pglnwidth.cc
  \date			Aug 2012
  \author		TNick

  \brief		Contains the implementation of PgLnWeight class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/layer/pglayglob.h>
#include	<cpg/logic/layer/pglayspc.h>
#include	<cpg/logic/pgctx.h>

#include	"pglnwidth.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PgLnWeight::PgLnWeight				( CpgDoc * doc )
	: PgCharacteristic( doc )
{
	weight_ = 1;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLnWeight::~PgLnWeight				( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLnWeight *			PgLnWeight::clone				( CpgDoc * destin, int & idx )
{
	Q_ASSERT( destin != NULL );
	PgLnWeight * ret = new PgLnWeight( destin );

	ret->weight_ = weight_;
	ret->setName( name() );

	destin->addLnWeight( ret );
	idx = destin->lnWeightCount() - 1;

	return ret;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qreal					PgLnWeight::explicitWeight		(
		PgCtx * ctx, int id )
{
	qreal		ret;

	for ( ;; )
	{
		switch	( id )	{
		case WEIGHT_SINGLE:		{
			ret = 1.0;
			break; }
		case WEIGHT_DOUBLE:		{
			ret = 2.0;
			break; }
		case WEIGHT_TRIPLE:		{
			ret = 3.0;
			break; }
		case WEIGHT_HALF:		{
			ret = 0.5;
			break; }
		case WEIGHT_QUARTER:	{
			ret = 0.25;
			break; }
		case BYLAYER:		{
			if ( ctx->layer_ == NULL )
			{
				Q_ASSERT( false );
				id = WEIGHT_SINGLE;
			}
			else
			{
				id = ctx->layer_->lineWeight();
				if ( id == BYLAYER )
				{
					Q_ASSERT( false );
					id = WEIGHT_SINGLE;
				}
			}
			continue;
		}
		case BYBLOCK:		{
			if ( ctx->block_def_ == NULL )
			{
				Q_ASSERT( false );
				id = WEIGHT_SINGLE;
			}
			else
			{
				/** @todo implement
				id = blk->lineWeight();
				blk = blk->block(); */
				Q_ASSERT( false );
				id = WEIGHT_SINGLE;
			}
			continue;
		}
		default:		{
			if ( id < 0 )
			{
				Q_ASSERT( false );
				id = WEIGHT_SINGLE;
				continue;
			}
			else
			{
				ret = ctx->document_->lnWeight( id )->weight();
				break;
			}
		}
		} /* switch	( id )	{ */
		break;
	} /* for ( ;; ) */
	return ret;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					PgLnWeight::asXML		(
		PgCtx * ctx, int id, QDomElement & dest )
{
/*
	dest.setAttribute( "pg-line-weight", id );
	dest.setAttribute(
				"pg-line-weight-explicit",
				explicitWeight( ctx, id )
				);
*/
	/** @todo asXML */
	Q_UNUSED( ctx );
	Q_UNUSED( id );
	Q_UNUSED( dest );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgLnWeight::isIdValid		( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->lnWeightCount() )
			return true;
		return false;
	}
	else
	{
		switch		( id )	{
		case	WEIGHT_SINGLE:
		case	WEIGHT_DOUBLE:
		case	WEIGHT_TRIPLE:
		case	WEIGHT_HALF:
		case	WEIGHT_QUARTER:
		case	BYLAYER:
		case	BYBLOCK:
			return true;
		default:
			return false;
		}
	}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgLnWeight::nameForId		( Id id )
{
	switch		( id )	{
	case	WEIGHT_SINGLE:
		return QObject::tr( "single" );
	case	WEIGHT_DOUBLE:
		return QObject::tr( "double" );
	case	WEIGHT_TRIPLE:
		return QObject::tr( "triple" );
	case	WEIGHT_HALF:
		return QObject::tr( "half" );
	case	WEIGHT_QUARTER:
		return QObject::tr( "quarter" );
	case	BYLAYER:
		return QObject::tr( "by layer" );
	case	BYBLOCK:
		return QObject::tr( "by block" );
	default:
		return QString();
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void				PgLnWeight::nameAndId		(
		QStringList & name_list, QList<int> & id_list )
{

#	define	nameAndId_helper(i)	\
	id_list.append( i ); name_list.append( nameForId( i ) );

	nameAndId_helper( BYLAYER );
	nameAndId_helper( BYBLOCK );
	nameAndId_helper( WEIGHT_SINGLE );
	nameAndId_helper( WEIGHT_DOUBLE );
	nameAndId_helper( WEIGHT_TRIPLE );
	nameAndId_helper( WEIGHT_HALF );
	nameAndId_helper( WEIGHT_QUARTER );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString				PgLnWeight::asString			( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->lnPattCount() )
			return doc->lnPatt( id )->name();
		return QString();
	}
	else
	{
		return nameForId( (Id)id );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int					PgLnWeight::copy			(
		CpgDoc * source, CpgDoc * destin, int value )
{
	Q_ASSERT( source != NULL );
	Q_ASSERT( destin != NULL );
	Q_ASSERT( isIdValid( source, value ) );

	/* can we directly use this value? */
	if ( source == destin )
		return value;

	if ( value >= 0 )
	{
		int i;
		/*PgLnWeight * new_col = */ source->lnWeight( value )->clone( destin, i );
		return i;
	}
	else
	{
		return value;
	}
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
